package com.backstage.service.Impl;

import com.backstage.mapper.SysPermissionMapper;
import com.backstage.pojo.dto.Message;
import com.backstage.pojo.entity.SysPermission;
import com.backstage.pojo.entity.SysUser;
import com.backstage.pojo.vo.SysPermissionVo;

import com.backstage.pojo.vo.SysUserVo;
import com.backstage.service.SysPermissionService;
import com.backstage.util.PojoUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("SyspermissionService")
public class SysPermissionServiceImpl implements SysPermissionService {
    @Autowired
    SysPermissionMapper sysPermissionMapper;

    @Override
    public Message<List<SysPermissionVo>> list(SysPermissionVo sysPermissionVo) {
        Page<SysPermission> page = new Page<>();
        page.setCurrent(sysPermissionVo.getPage());
        page.setSize(sysPermissionVo.getRows());
        QueryWrapper<SysPermission> wrapper = new QueryWrapper<>();
        IPage<SysPermission> resultPage = sysPermissionMapper.selectPage(page, wrapper);
        List<SysPermissionVo> result = new ArrayList<>();
        for (int i = 0; i < resultPage.getRecords().size(); i++) {
            SysPermission sysPermission = resultPage.getRecords().get(i);
            SysPermissionVo permission = PojoUtil.copyObject(sysPermission, SysPermissionVo.class);
            permission.setPage(sysPermissionVo.getPage());
            permission.setRows(sysPermissionVo.getRows());
            result.add(permission);
        }
        Message<List<SysPermissionVo>> message = Message.successMessage(resultPage.getTotal(), result);
        return message;
    }

    @Override
    public Message<SysPermissionVo> edit(SysPermissionVo sysPermissionVo) {
        SysPermission sysPermission = PojoUtil.copyObject(sysPermissionVo, SysPermission.class);
        int i = sysPermissionMapper.updateById(sysPermission);//根据ID更新book表
        if (i == 0) return Message.errorMessage("1001", "更新失败");
        else return Message.successMessage(1L, sysPermissionVo);
    }

    @Override
    public Message<SysPermissionVo> add(SysPermissionVo sysPermissionVo) {
        SysPermission sysPermission = PojoUtil.copyObject(sysPermissionVo, SysPermission.class);
        int i = sysPermissionMapper.insert(sysPermission);
        if (i == 0) return Message.errorMessage("1001", "添加失败");
        else return Message.successMessage(1L, sysPermissionVo);
    }

    @Override
    public Message<SysPermissionVo> delete(Integer id) {
        int i = sysPermissionMapper.deleteById(id);
        if (i == 0) return Message.errorMessage("1001", "删除失败");
        else return Message.successMessage(1L, null);
    }

    @Override
    public Message<SysPermissionVo> get(Long id) {
        QueryWrapper<SysPermission> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        SysPermission sysPermission = sysPermissionMapper.selectOne(wrapper);
        return Message.successMessage(1L, PojoUtil.copyObject(sysPermission, SysPermissionVo.class));

    }
}
